package com.tepth.busfix.main.partsearch;

import com.tepth.busfix.bean.reponse.SearchPartResult;
import com.tepth.busfix.bean.reponse.TroubleContent;
import com.tepth.busfix.network.ApiModel;
import com.tepth.busfix.network.observer.SimpleDisposableObserver;

import java.util.ArrayList;

/**
 * Created by TMVPHelper on 2017/03/17
 */
public class PartSearchPresenter extends PartSearchContract.Presenter {

    @Override
    void search() {
        mRxManager.add(mModel.searchPart(mView.getAuthKey(), mView.getName()).
                subscribeWith(new SimpleDisposableObserver<ApiModel<SearchPartResult>>(mView) {

                    @Override
                    public void onSucceed(Object o) {
                        SearchPartResult searchPartResult = (SearchPartResult) o;
                        ArrayList<TroubleContent.Part> listAccessory = searchPartResult.getListAccessory();
                        ArrayList<TroubleContent.Part> listComponent = searchPartResult.getListComponent();

                        int accessorySize = listAccessory == null ? 0 : listAccessory.size();
                        int componentSize = listComponent == null ? 0 : listComponent.size();
                        ArrayList<TroubleContent.Part> tempList = new ArrayList<>();
                        if (accessorySize > 0) {
                            tempList.addAll(listAccessory);
                        }
                        if (componentSize > 0) {
                            tempList.addAll(listComponent);
                        }
                        mView.showSearchResult(accessorySize, componentSize, tempList);
                    }
                }));
    }

    @Override
    void getCommonComponents() {
        mRxManager.add(mModel.getCommonComponents(mView.getAuthKey(), mView.getCount()).
                subscribeWith(new SimpleDisposableObserver<ApiModel<SearchPartResult>>(mView) {

                    @Override
                    public void onSucceed(Object o) {
                        SearchPartResult searchPartResult = (SearchPartResult) o;
                        ArrayList<TroubleContent.Part> listAccessory = searchPartResult.getListAccessory();
                        ArrayList<TroubleContent.Part> listComponent = searchPartResult.getListComponent();

                        int accessorySize = listAccessory == null ? 0 : listAccessory.size();
                        int componentSize = listComponent == null ? 0 : listComponent.size();
                        ArrayList<TroubleContent.Part> tempList = new ArrayList<>();
                        if (accessorySize > 0) {
                            tempList.addAll(listAccessory);
                        }
                        if (componentSize > 0) {
                            tempList.addAll(listComponent);
                        }
                        mView.showCommonComponents(accessorySize, componentSize, tempList);
                    }
                }));
    }
}